﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;

namespace sharpxbot.Core.Configuration
{
    public class XmlConfiguration : IConfiguration
    {
        public const int DefaultTrafficLimit = 100000; //in bytes

        #region IConfiguration Members

        public string UserName { get; private set; }

        public string Password { get; private set; }

        public string Server { get; private set; }

        public string PluginsPath { get; private set; }

        public string PluginsDataPath { get; private set; }

        public string OwnerJid { get; private set; }

        public int TrafficLimit { get; private set; }

        public ICollection<IGroupChatConfiguration> Rooms { get; private set; }

        #endregion

        public XmlConfiguration(string fileName)
        {
            if (!File.Exists(fileName))
                throw new ArgumentException("Illegal configuration file. File doesn't exist");

            Rooms = new List<IGroupChatConfiguration>();
            TrafficLimit = DefaultTrafficLimit;

            ParseConfiguration(fileName);
        }

        private void ParseConfiguration(string fileName)
        {
            using (FileStream fs = new FileStream(fileName, FileMode.Open))
            {
                XDocument document = XDocument.Load(fs);
                var generalOptions = document.Descendants("general").Descendants();

                foreach (XElement element in generalOptions)
                {
                    switch (element.Name.LocalName)
                    {
                        case "username":
                            UserName = element.Value;
                            break;
                        case "password":
                            Password = element.Value;
                            break;
                        case "server":
                            Server = element.Value;
                            break;
                        case "plugins_path":
                            PluginsPath = element.Value;
                            break;
                        case "plugins_data_path":
                            PluginsDataPath = element.Value;
                            break;
                        case "owner_jid":
                            OwnerJid = element.Value;
                            break;
                    }
                }

                var roomsOptions = document.Descendants("rooms").Elements();

                foreach (XElement element in roomsOptions)
                {
                    IGroupChatConfiguration roomConfig = new XmlGroupChatConfiguration(element);
                    Rooms.Add(roomConfig);
                }

                var trafficLimitOptions = document.Descendants("traffic_limit").Elements();

                foreach (XElement element in trafficLimitOptions)
                {
                    switch (element.Name.LocalName)
                    {
                        case "value":
                            {
                                int ival = -1;
                                if (Int32.TryParse(element.Value, out ival))
                                {
                                    TrafficLimit = ival;
                                }
                            }
                            break;
                    }
                }
            }
        }
    }
}
